<template>
  <div class="dashboard-container">
    <div class="app-container">
    <el-card shadow="never">
      <el-row type="flex" justify="space-between">
        <el-col>
            <span><strong>学科名称</strong></span>
            <el-input v-model="subForm.subjectName" style="width:200px" class="inputClass"></el-input>
          <el-button size="small" @click="clean">清除</el-button>
          <el-button size="small" type="primary" @click="search">搜索</el-button>
        </el-col>
        <el-col :span="2">
          <el-button @click="SubjectAdd" size="small" type="success"><i class="el-icon-edit"></i>新增学科</el-button>
        </el-col>
      </el-row>

        <el-alert
          title=""
          type="info"
          :closable="false"
          show-icon
          >
          <span slot='title'>一共有{{total}}条数据</span>
      </el-alert>

      <el-table  stripe :data='subjectList'>
        <el-table-column  label="序号" type="index"></el-table-column>
        <el-table-column property="subjectName" label="学科名称" ></el-table-column>
        <el-table-column property="username"  label="创建者" ></el-table-column>
        <el-table-column property="addDate"  label="创建日期">
          <template #default="{row}">
            {{row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column property="isFrontDisplay" label="前台是否显示" >
          <template #default="{row}">
            <span v-if='+row.isFrontDisplay===1'>是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column property="twoLevelDirectory"  label="二级目录" ></el-table-column>
        <el-table-column property="tags"  label="标签" ></el-table-column>
        <el-table-column property="totals" label="题目数量" ></el-table-column>
        <el-table-column label="操作" fixed="right" width="240px" >
          <template #default="{row}"  >
          <el-button type="text" size="small" @click="directorys(row)">学科分类</el-button>
          <el-button type="text" size="small" @click="tags(row)">学科标签</el-button>
          <el-button type="text" size="small" @click="editSub(row.id)">修改</el-button>
          <el-button type="text" size="small" @click="delSub(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="subjectObj.page"
        :page-sizes="[5, 10, 20,50]"
        :page-size="subjectObj.pagesize"
        layout="prev, pager, next,sizes, jumper"
        :total="total">
      </el-pagination>
    </el-row>

    </el-card>

  <SubjectDialog ref="SubjectDialogRef" :showDialog.sync="showDialog" @updateList="loadList"></SubjectDialog>

  </div>
  </div>
</template>

<script>

import { getSubjectList, remove } from '@/api/hmmm/subjects'
// import { parseTimeByString } from '@/filters'
import SubjectDialog from '../components/subjects-add.vue'

export default {
  name: 'SubjectPage',
  components: {
    SubjectDialog
  },
  data () {
    return {
      subjectList: [],
      subForm: {
        subjectName: ''
      },
      subjectObj: {
        page: 1,
        pagesize: 10
      },
      total: 10,
      showDialog: false
    }
  },
  created () {
    this.loadList()
  },

  methods: {
    async loadList () {
      const res = await getSubjectList(this.subjectObj)
      // console.log(res)
      this.subjectList = res.data.items
      // console.log(this.subjectList)
      this.total = res.data.counts
    },
    handleSizeChange (size) {
      this.subjectObj.pagesize = size
      this.loadList()
    },
    handleCurrentChange (page) {
      this.subjectObj.page = page
      this.loadList()
    },
    SubjectAdd () {
      // console.log(1)
      this.showDialog = true
    },
    async delSub (id) {
      try {
        await this.$confirm('您确定要删除嘛')
        await remove(id)
        const totalPage = Math.ceil((this.total - 1) / this.subjectObj.pagesize)
        this.subjectObj.page = this.subjectObj.page > totalPage ? totalPage : this.subjectObj.page
        this.subjectObj.page = this.subjectObj.page < 1 ? 1 : this.subjectObj.page
        this.loadList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    async editSub (id) {
      await this.$refs.SubjectDialogRef.subDetail(id)
      this.showDialog = true
    },
    async clean () {
      this.subForm.subjectName = ''
      await this.loadList()
    },
    async search () {
      if (this.subForm.subjectName) {
        this.subjectObj.page = 1
        const res = await getSubjectList({ ...this.subjectObj, subjectName: this.subForm.subjectName })
        console.log(res)
        this.subjectList = res.data.items
        this.total = res.data.counts
        this.$message.success('查询成功')
      } else {
        this.$message.warning('请输入内容')
      }
    },
    directorys (row) {
      // this.$router.push('directorys')
      this.$router.push({ path: '/subjects/directorys', query: { id: row.id, name: row.subjectName } })
    },
    tags (row) {
      // this.$router.push('tags')
      this.$router.push({ path: '/subjects/tags', query: { id: row.id, name: row.subjectName } })
    }
  }
}
</script>

<style scoped lang='scss'>
.container{
    padding: 20px;
}
.el-card{
  .el-icon-edit{
    margin-right: 10px;
  }
  .inputClass{
    margin: 0 10px;
  }
  .el-alert{
    margin:20px 0;
  }
}
</style>
